package com.joysuch.wwyt.patrol.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.joysuch.wwyt.attendance.service.AttendanceApiUserService;
import com.joysuch.wwyt.common.constant.Constant;
import com.joysuch.wwyt.core.bean.SimpleListBean;
import com.joysuch.wwyt.core.enums.DeleteFlag;
import com.joysuch.wwyt.core.repository.BaseUserDao;
import com.joysuch.wwyt.patrol.entity.PatrolCheckPlan;
import com.joysuch.wwyt.patrol.entity.PatrolTask;
import com.joysuch.wwyt.patrol.entity.PatrolTaskAssign;
import com.joysuch.wwyt.patrol.entity.dto.TaskAssignDto;
import com.joysuch.wwyt.patrol.mapper.PatrolTaskAssignMapper;
import com.joysuch.wwyt.patrol.service.PatrolTaskAssignService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joysuch.wwyt.patrol.service.PatrolTaskService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author heqifeng
 * @since 2022-03-01
 */
@Service
public class PatrolTaskAssignServiceImpl extends ServiceImpl<PatrolTaskAssignMapper, PatrolTaskAssign> implements PatrolTaskAssignService {

    @Autowired
    private AttendanceApiUserService attendanceApiUserService;
    @Autowired
    private PatrolTaskService taskService;
    @Autowired
    private PatrolTaskAssignMapper assignMapper;
    @Autowired
    private BaseUserDao userDao;

    @Override
    public List<PatrolTaskAssign> genAssignByPlanAndTask(PatrolCheckPlan plan, Long taskId, Date startTime) {
        String checkUserIdStr = plan.getCheckUserIds();
        if (StringUtils.isNotBlank(checkUserIdStr)) {
            List<Long> assignIdList = Splitter.on(Constant.SPLIT_COMMA)
                                      .trimResults()
                                      .splitToList(checkUserIdStr)
                                      .stream()
                                      .map(Long::new)
                                      .collect(Collectors.toList());
            if (CollectionUtil.isEmpty(assignIdList)) {
                return null;
            }
            // 过滤删除掉的人员
            List<Long> effectiveUsers = userDao.filterDeleteUser(assignIdList);
            if (CollectionUtil.isEmpty(effectiveUsers)) {
                return null;
            }
            List<PatrolTaskAssign> assignList = new ArrayList<>();
            // 校验排班信息,如果无人上班，则分配给所有人
            List<Long> realAssigns = attendanceApiUserService.userTaskCheck(startTime, effectiveUsers);
            if (CollectionUtil.isEmpty(realAssigns)) {
                realAssigns.addAll(effectiveUsers);
            }
            realAssigns.forEach(each -> {
                PatrolTaskAssign assign = new PatrolTaskAssign();
                assign.setTaskId(taskId)
                        .setUserId(each);
                assign.setTenentId(plan.getTenentId());
                assign.setOrgCode(plan.getOrgCode());
                assignList.add(assign);
            });
            if (CollectionUtil.isNotEmpty(assignList)) {
                return assignList;
            }else {
                // 任务无分配人，直接物理删除
                taskService.removeById(taskId);
            }
        }
        return new ArrayList<>();
    }

    @Override
    public List<SimpleListBean> getAssignByTaskId(Long taskId) {
        return assignMapper.selectSimpleAssignByTaskId(taskId);
    }

    @Override
    public List<TaskAssignDto> getAssignByTaskIds(Set<Long> taskIds) {
        return assignMapper.selectSimpleAssignByTaskIds(taskIds);
    }

    @Override
    public Integer batchSave(List<PatrolTaskAssign> assigns, Integer batchSize) {
        Integer insertCount = 0;
        List<List<PatrolTaskAssign>> assignPages = Lists.partition(assigns, batchSize);
        for(List<PatrolTaskAssign> assignPage : assignPages) {
            assignMapper.batchInsert(assignPage);
            insertCount += assignPage.size();
        }
        return insertCount;
    }
}
